package com.ruoyi.biz.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.biz.system.domain.BizArea;
import com.ruoyi.biz.system.mapper.BizAreaMapper;
import com.ruoyi.biz.system.service.IBizAreaService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 区域Service业务层处理
 *
 * @author Fei.Cong
 * @date 2023-05-29
 */
@Service
public class BizAreaServiceImpl extends ServiceImpl<BizAreaMapper, BizArea> implements IBizAreaService {

    @Autowired
    private BizAreaMapper bizAreaMapper;

    @Autowired
    ISysDeptService sysDeptService;

    /**
     * 分页查询区域
     *
     * @param page
     * @param bizArea 区域
     */
    @Override
    public IPage<BizArea> pageList(BizArea bizArea, IPage<BizArea> page) {
        return bizAreaMapper.selectPageList(page, bizArea);
    }

    /**
     * 根据区域id查询所属部门id
     *
     * @param areaId
     * @return
     */
    @Override
    public Long getDeptIdByAreaCode(String areaId) {
        BizArea area = bizAreaMapper.selectOne(new LambdaQueryWrapper<BizArea>()
                .eq(BizArea::getAreaId, areaId));
        if (Objects.nonNull(area)) {
            SysDept sysDept = new SysDept();
            sysDept.setDeptName(area.getName());
            List<SysDept> depts = sysDeptService.listAllDept(sysDept);
            if (CollectionUtil.isNotEmpty(depts)) {
                return depts.get(0).getDeptId();
            }
        }
        return 100L;
    }

}
